-
Notifications
You must be signed in to change notification settings - Fork 163
fix(langchain): anthropic cache token count #2414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
...eninference-instrumentation-langchain/src/openinference/instrumentation/langchain/_tracer.py
Show resolved
Hide resolved
| "cache_read_input_tokens" in obj | ||
| and isinstance(obj["cache_read_input_tokens"], int) | ||
| or "cache_creation_input_tokens" in obj | ||
| and isinstance(obj["cache_creation_input_tokens"], int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Add explicit parentheses in the type guard for clarity
return (
"input_tokens" in obj
and "output_tokens" in obj
and isinstance(obj["input_tokens"], int)
and isinstance(obj["output_tokens"], int)
and (
("cache_read_input_tokens" in obj and isinstance(obj["cache_read_input_tokens"], int))
or ("cache_creation_input_tokens" in obj and isinstance(obj["cache_creation_input_tokens"], int))
)
)| ) | ||
| ): | ||
| return | ||
| keys: Sequence[str] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Anthropic Usage Metrics Disappear
Removing input_tokens and output_tokens from the generic token count extraction breaks basic Anthropic responses that don't include cache tokens or total_tokens. Previously, responses with just input_tokens and output_tokens (standard Anthropic format without caching) would be captured by the first loop. Now they're only handled by _is_raw_anthropic_usage_with_cache_read_or_write, which requires cache tokens to be present, causing basic Anthropic token counts to be lost.
resolves #2381
Note
Improve token usage extraction to handle Anthropic cache read/write and LangChain UsageMetadata (with Bedrock heuristics), update deps, and add targeted tests.
_tracer.py):_is_lc_usage_metadata/_token_counts_from_lc_usage_metadatato maplangchain_core.messages.ai.UsageMetadata, including audio/reasoning and cache details with Bedrock-specific heuristics._is_raw_anthropic_usage_with_cache_read_or_write/_token_counts_from_raw_anthropic_usage_with_cache_read_or_writeto handle Anthropiccache_read_input_tokens/cache_creation_input_tokensand emit detailed cache attributes._token_counts; remove previous key-based Anthropic handling.TypedDict,TypeGuard,UsageMetadata).tests/test_token_counts.pycovering LC usage metadata and Anthropic cache scenarios.tests/test_instrumentor.pyAnthropic expectations (LLM_TOKEN_COUNT_PROMPTfrom22to33).langchain_coreto>= 0.3.9(instruments) and== 0.3.9(type-check).tox.ini: adduv pip list -vincommands_pre.Written by Cursor Bugbot for commit 30a6e95. This will update automatically on new commits. Configure here.